package com.ph.member.api.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.member.api.controller.qo.MemberAuthQo;
import com.ph.member.api.service.MemberAuthDataService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.date.ThreadLocalDateUtil;
import com.ph.shopping.common.util.excel.ExcelBean;
import com.ph.shopping.common.util.excel.ExcelUtil;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.member.dto.CheckMemberAuthDto;
import com.ph.shopping.facade.member.dto.QueryMemberAuthDto;
import com.ph.shopping.facade.member.service.IMemberAuthService;
import com.ph.shopping.facade.member.vo.MemberAuthVo;

/**
 * 推广师controller
 * @author zhengpeng
 *
 */
@Controller
@RequestMapping("api/memberauth")
public class MemberAuthController extends BaseController {
	
	private static Logger logger = LoggerFactory.getLogger(MemberAuthController.class);
	
	private static String pattern = "yyyy-MM-dd HH:mm:ss";

	@Reference(version = "1.0.0", retries = 1, timeout = 10000)
	IMemberAuthService memberAuthService;
	
	@Autowired
	MemberAuthDataService memberAuthDataService;
	
	
	/**
	 * 跳转列表页面
	 * @return
	 */
	@RequestMapping(value="/listPage",method=RequestMethod.GET)
	public String listPage(){
		return "/memberauth/list";
	}
	
	/**
	 * 分页获取推广师列表
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/list",method= RequestMethod.POST)
	@ResponseBody
	public Object list(@ModelAttribute PageBean page,@ModelAttribute MemberAuthQo memberAuthQo){
		logger.info("分页获取推广师列表入参，page={},memberAuthQo={}",JSON.toJSONString(page),JSON.toJSONString(memberAuthQo));
		try {
			QueryMemberAuthDto queryMemberAuthDto = new QueryMemberAuthDto();
			BeanUtils.copyProperties(memberAuthQo, queryMemberAuthDto);
			if (StringUtils.isNotBlank(memberAuthQo.getCreateTimeStr())) {
				queryMemberAuthDto.setCreateTimeStr(ThreadLocalDateUtil.strToDate(pattern, memberAuthQo.getCreateTimeStr()+" 00:00:00"));
			}
			if (StringUtils.isNotBlank(memberAuthQo.getCreateTimeEnd())) {
				queryMemberAuthDto.setCreateTimeEnd(ThreadLocalDateUtil.strToDate(pattern, memberAuthQo.getCreateTimeEnd() + " 23:59:59"));
			}
			logger.info("分页获取推广师列表接口入参，memberPageDto={}",JSON.toJSONString(queryMemberAuthDto));
			Result result = memberAuthService.getMemberAuthListByPage(page,queryMemberAuthDto);
			logger.info("分页获取推广师列表返回值，result={}",JSON.toJSONString(result));
			return result;
		} catch (Exception e) {
			logger.error("分页获取推广师列表异常，ex={}", e);
		}
		return getResult(ResultEnum.INTERNAL_SERVER_ERROR);	
	}
	
	/**
	 * 推广师审核
	 * @return
	 */
	@RequestMapping(value="/check",method= RequestMethod.POST)
	@ResponseBody
	public Object check(Long memberId, Integer status){
		logger.info("推广师审核入参，memberId={},status={}", memberId, status);
		try {
			CheckMemberAuthDto checkMemberAuthDto = new CheckMemberAuthDto();
			checkMemberAuthDto.setMemberID(memberId);
			checkMemberAuthDto.setStatus(status);
			logger.info("推广师审核接口入参，memberPageDto={}",JSON.toJSONString(checkMemberAuthDto));
			Result result = memberAuthService.check(checkMemberAuthDto);
			logger.info("推广师审核返回值，result={}",JSON.toJSONString(result));
			return result;
		} catch (Exception e) {
			logger.error("推广师审核异常，ex={}", e);
		}
		return getResult(ResultEnum.INTERNAL_SERVER_ERROR);	
	}
	
	/**
	 * 
	* @Title: exportExcel  
	* @Description: 导出  
	* @param @param request
	* @param @param dto    参数  
	* @return void    返回类型  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/memberAuthExport")
	public void exportExcel(HttpServletRequest request,HttpServletResponse response,@ModelAttribute MemberAuthQo memberAuthQo){
		logger.info("导出推广师导出参数 MemberPageDto = {} " ,JSON.toJSONString(memberAuthQo));
		boolean flag = false;
		try {
			QueryMemberAuthDto queryMemberAuthDto = new QueryMemberAuthDto();
			BeanUtils.copyProperties(memberAuthQo, queryMemberAuthDto);
			if (StringUtils.isNotBlank(memberAuthQo.getCreateTimeStr())) {
				queryMemberAuthDto.setCreateTimeStr(ThreadLocalDateUtil.strToDate(pattern, memberAuthQo.getCreateTimeStr()+" 00:00:00"));
			}
			if (StringUtils.isNotBlank(memberAuthQo.getCreateTimeEnd())) {
				queryMemberAuthDto.setCreateTimeEnd(ThreadLocalDateUtil.strToDate(pattern, memberAuthQo.getCreateTimeEnd() + " 23:59:59"));
			}
			
			logger.info("导出推广师导出参数 queryMemberAuthDto = {} " ,JSON.toJSONString(queryMemberAuthDto));
			Result result = memberAuthService.getMemberAuthList(queryMemberAuthDto);
			logger.info("导出推广师导出返回数据 Result = {} " ,JSON.toJSONString(result));
			if(result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())){
				Object obj = result.getData();
				ExcelBean exelBean = memberAuthDataService.getMemberAuthExcelBean((List<MemberAuthVo>)obj);
				ExcelUtil.download(request, response, exelBean);
				flag = true;
			}
		} catch (Exception e) {
			logger.error("导出推广师数据列表错误",e);
		}
		if(flag){
			ExcelUtil.responseMsg(response, "导出推广师数据失败");
		}
	}
}
